<?php
namespace app\controller\admin;

use app\BaseController;
use think\facade\Request;
use app\model\GoodsModel;
use app\model\CommentModel;
use app\model\tool\ImageModel;

class GoodsComment extends BaseController
{


    public function get_list(){


        $_GPC = Request::param();
        $filter = [];
        if( isset($_GPC['author']) ){
            $filter['author'] = $_GPC['author'];
        }

        if( isset($_GPC['status']) ){
            $filter['status'] = $_GPC['status'];
        }

        if( isset($_GPC['date']) ){
            $filter['date'] = $_GPC['date'];
        }

        if( isset($_GPC['goods_name']) ){
            $filter['goods_name'] = $_GPC['goods_name'];
        }

        if( isset($_GPC['goods_id']) ){
            $filter['goods_id'] = $_GPC['goods_id'];
        }

        $list = CommentModel::get_list( $filter );

        foreach( $list as &$comment ){

            $comment['goods_image'] = ImageModel::resize2($comment['goods_image'],50,50);
            $comment['date'] = Date('Y-m-d', strtotime($comment['date']));

        }

        return json( $list );


    }

    public function edit(){

        // global $_GPC, $_W;
        $_GPC = Request::param();
        $comment_id = $_GPC['id']??0;
        $res = [
            'status'=>0,
        ];

        $images = [];
        $img_path_json = [];

        // if( !empty( $_POST['images_0'] ) ){
        //     $images_0 = $_POST['images_0'] ;
        //     $images_0 = htmlspecialchars_decode( $images_0 );
        //     if( ImageModel::is_base64( $images_0) ){

        //         $result = ImageModel::save_base64( $images_0,time().'-0' );
        //         if( $result ) {
        //             $img_path_json[] = $result;
        //         }
        //     }else {
        //         $img_path_json[] = $images_0;
        //     }
        // }
        // if( !empty( $_POST['images_1'] ) ){
        //     $images_1 = $_POST['images_1'] ;
        //     $images_1 = htmlspecialchars_decode( $images_1 );
        //     if( ImageModel::is_base64( $images_1) ){

        //         $result = ImageModel::save_base64( $images_1, time().'-1' );
        //         if( $result ) {
        //             $img_path_json[] = $result;
        //         }
        //     }else {
        //         $img_path_json[] = $images_1;
        //     }
        // }
        // if( !empty( $_POST['images_2'] ) ){
        //     $images_2 = $_POST['images_2'] ;
        //     $images_2 = htmlspecialchars_decode( $images_2 );
        //     if( ImageModel::is_base64( $images_2) ){

        //         $result = ImageModel::save_base64( $images_2, time().'-2' );
        //         if( $result ) {
        //             $img_path_json[] = $result;
        //         }
        //     }else {
        //         $img_path_json[] = $images_2;
        //     }
        // }

        $data = [
            'author'        => $_GPC['author'],
            'avatar'        => $_GPC['avatar'],
            'customer_id'   => $_GPC['customer_id']?:0,
            'goods_id'      => $_GPC['goods_id'],
            'content'       => $_GPC['content'],
            'score'         => $_GPC['score'],
            // 'images'        => json_encode( $img_path_json, true),
            'images'        => json_encode( $img_path_json ),
            'date'          => $_GPC['date']?:Date('Y-m-d'),
            'status'        => $_GPC['status']
        ];

        $edit = CommentModel::edit( $comment_id, $data );

        if( !$edit){
            $res['status'] = 1;
            $res['message'] = '保存失败';
        }

        $res['edit'] = $edit;
        return json( $res );

    }

    public function single(){

        $id = $_GET['id'];
        $json = [
            'status'=>0
        ];

        if( empty( $id ) ){
            $json['status'] = 1;
            return json( $json );
        }

        $comment = CommentModel::single( $id );

        $comment['date'] = Date('Y-m-d', strtotime( $comment['date'] ) );
        $comment['goods'] = goodsModel::info( $comment['goods_id'] );

        $comment['img_show'] = [];
        if( !empty($comment['images']) ){
            $images = json_decode( $comment['images'], true );
            $comment['images'] = $images;
            foreach( $images as $img ){
                $comment['img_show'][] = [
                    'url' => ImageModel::resize2( $img ),
                    'path' => $img,
                ];
            }
        }

        return json( $comment );

    }

    public function remove(){

        $ids = $_POST['ids'];

        $ret = CommentModel::remove( $ids );

        return json([
            'status'=>0,
            'ret'=>$ret,
        ]);
    }

}
